﻿using AmuTools.Web;
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace AmuTools.Ali
{
    public class AliWuliu
    {
        private string host = "https://wuliu.market.alicloudapi.com";
        public string AppCode { get; private set; }
        public AliWuliu(string appCode)
        {
            AppCode = appCode;
        }

        public AliWuliuResult<QueryResult> Qyery(string no, string type = null)
        {
            string url = host + "/kdi";

            string querys = $"no={no}";
            if (type != null) querys += $"&type={type}";

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            WebRequestHelper webRequest = new WebRequestHelper(url);
            webRequest.HttpWebRequest.Headers.Add("Authorization", "APPCODE " + AppCode);
            webRequest.GetResponse();
            var result = webRequest.ReadResponse<AliWuliuResult<QueryResult>>();
            return result;

            //string bodys = "";
            //HttpWebRequest httpRequest = null;
            //HttpWebResponse httpResponse = null;

            //if (host.Contains("https://"))
            //{
            //    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            //    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            //}
            //else
            //{
            //    httpRequest = (HttpWebRequest)WebRequest.Create(url);
            //}
            //httpRequest.Method = "Get";
            //httpRequest.Headers.Add("Authorization", "APPCODE " + AppCode);
            //if (0 < bodys.Length)
            //{
            //    byte[] data = Encoding.UTF8.GetBytes(bodys);
            //    using (Stream stream = httpRequest.GetRequestStream())
            //    {
            //        stream.Write(data, 0, data.Length);
            //    }
            //}
            //try
            //{
            //    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            //}
            //catch (WebException ex)
            //{
            //    httpResponse = (HttpWebResponse)ex.Response;
            //}

            //Console.WriteLine(httpResponse.StatusCode);
            //Console.WriteLine(httpResponse.Method);
            //Console.WriteLine(httpResponse.Headers);
            //Stream st = httpResponse.GetResponseStream();
            //StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            //Console.WriteLine(reader.ReadToEnd());
            //Console.WriteLine("\n");

        }

        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }

    public class AliWuliuQueryResult: AliWuliuResult<QueryResult> { }

    public class AliWuliuResult<T>
    {
        public string status { get; set; }
        public string msg { get; set; }
        public T result { get; set; }
    }

    public class QueryResult
    {
        public string number { get; set; }
        public string type { get; set; }
        public List<QueryResultItem> list { get; set; }

        public class QueryResultItem
        {
            public string time { get; set; }
            public string status { get; set; }
        }
    }
}
